//
//  ThirdLoginShareVerifyManager.m
//  YJAPPThird
//
//  Created by 张吉旭 on 16/12/20.
//  Copyright © 2016年 yj. All rights reserved.
//

#import "ThirdLoginShareVerifyManager.h"
#import "ThirdDefine.h"
#import <UMMobClick/MobClick.h>
#import <UMSocialCore/UMSocialCore.h>



static ThirdLoginShareVerifyManager * instance = nil;
@implementation ThirdLoginShareVerifyManager


+(ThirdLoginShareVerifyManager*)shareInstance{
    static dispatch_once_t once_Token;
    dispatch_once(&once_Token, ^{
        instance = [[ThirdLoginShareVerifyManager alloc] init];
    });
    
    return instance;
}

-(void)setUMeng:(NSDictionary*)launchOptions{
    UMConfigInstance.appKey = UMengAppKey;
//    UMConfigInstance.channelId = UMengChannel;
    [MobClick startWithConfigure:UMConfigInstance];//配置以上参数后调用此方法初始化SDK！
    
    [self setUpUmengShare];
}


//注册友盟分享
-(void)setUpUmengShare{
    
    //是否打开调试日志
    [[UMSocialManager defaultManager] openLog:NO];
    //设置友盟Appkey
    [[UMSocialManager defaultManager] setUmSocialAppkey:UMengAppKey];
    //当前网络请求是否用https
    [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
    
    //设置微信的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx8e4802fd8419f8d5" appSecret:@"17b0816d8e90fa1b8a3ba56428aaf947" redirectURL:@"http://www.yjsale.com"];
    
    //设置分享到QQ互联的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1105762818"  appSecret:nil redirectURL:@"http://www.yjsale.com"];
    
    //设置新浪的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"447140206"  appSecret:@"f743fe2478e1345e0a9c4409a6d34830" redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
}



-(BOOL)handleOpenURL:(NSURL*)url{
    return [[UMSocialManager defaultManager] handleOpenURL:url];
}


//授权并获取用户信息
- (void)getUserInfoForPlatform:(THIRDPLATFORM)platformType currentViewController:(UIViewController*)currentViewController userInfoResult:(platformResult)userInfoResult;
{
    UMSocialPlatformType type = UMSocialPlatformType_WechatSession;
    switch (platformType) {
        case 0:
            type = UMSocialPlatformType_Sina;//新浪
            break;
        case 1:
            type = UMSocialPlatformType_WechatSession;//微信聊天
            break;
        case 2:
            type = UMSocialPlatformType_WechatTimeLine;//微信朋友圈
            break;
        case 3:
            type = UMSocialPlatformType_WechatFavorite;//微信收藏
            break;
        case 4:
            type = UMSocialPlatformType_QQ;//QQ聊天页面
            break;
        case 5:
            type = UMSocialPlatformType_Qzone;//qq空间
            break;
            
        default:
            break;
    }
    
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:type currentViewController:currentViewController completion:^(id result, NSError *error) {
        UMSocialUserInfoResponse *umResult = result;
        
        ShareUserInfoModel *userinfo = [[ShareUserInfoModel alloc] init];
        userinfo.uid = umResult.uid;
        userinfo.openid = umResult.openid?:umResult.accessToken;
        userinfo.name = umResult.name;
        userinfo.iconurl = umResult.iconurl;
        userinfo.gender = umResult.gender;
        
        if (userInfoResult) {
            userInfoResult(userinfo,error);
        }
    }];
}

- (void)cancelAuthWithPlatform:(THIRDPLATFORM)platformType
{
    UMSocialPlatformType type = UMSocialPlatformType_WechatSession;
    switch (platformType) {
        case 0:
            type = UMSocialPlatformType_Sina;//新浪
            break;
        case 1:
            type = UMSocialPlatformType_WechatSession;//微信聊天
            break;
        case 2:
            type = UMSocialPlatformType_WechatTimeLine;//微信朋友圈
            break;
        case 3:
            type = UMSocialPlatformType_WechatFavorite;//微信收藏
            break;
        case 4:
            type = UMSocialPlatformType_QQ;//QQ聊天页面
            break;
        case 5:
            type = UMSocialPlatformType_Qzone;//qq空间
            break;
            
        default:
            break;
    }
    
    [[UMSocialManager defaultManager] cancelAuthWithPlatform:type completion:^(id result, NSError *error) {
        
    }];

}

-(BOOL) isInstall:(THIRDPLATFORM)platformType
{
    
    UMSocialPlatformType type = UMSocialPlatformType_WechatSession;
    switch (platformType) {
        case 0:
            type = UMSocialPlatformType_Sina;//新浪
            break;
        case 1:
            type = UMSocialPlatformType_WechatSession;//微信聊天
            break;
        case 2:
            type = UMSocialPlatformType_WechatTimeLine;//微信朋友圈
            break;
        case 3:
            type = UMSocialPlatformType_WechatFavorite;//微信收藏
            break;
        case 4:
            type = UMSocialPlatformType_QQ;//QQ聊天页面
            break;
        case 5:
            type = UMSocialPlatformType_Qzone;//qq空间
            break;
            
        default:
            break;
    }

    return [[UMSocialManager defaultManager] isInstall:type];
}


@end
